<template>
	<view class="page">
		<u-navbar title="在线留言" :auto-back="true">
			<view class="u-nav-slot" slot="right">
				<view class="nav-right" @click="openPopUp">
					<!-- <u-icon name="search" size="24" color="#376CB4"></u-icon> -->
					<u-icon name="list" size="24" style="margin-left: 20rpx;" color="#000"></u-icon>
				</view>
			</view>
		</u-navbar>
		<view class="ll"></view>

		<view class="add-box">
			<u--form labelPosition="top" :rules="rules" labelWidth="100" :model="params" ref="uForm" errorType="toast">
				<u-form-item label="标题" prop="name" borderBottom ref="item1">
					<u--input placeholder="请输入标题" v-model="params.name" border="none"></u--input>
				</u-form-item>
				<u-form-item label="姓名" prop="contact" borderBottom ref="item1">
					<u--input placeholder="请输入姓名" border="none" v-model="params.contact"></u--input>
				</u-form-item>
				<u-form-item label="验证码" prop="code" borderBottom ref="item1">
					<view
						style="width: 100%;display: flex;flex-direction: row;align-items: center;justify-content: space-between;">
						<view style="width: 60%;">
							<u--input border="none" v-model="params.code" placeholder="请输入验证码" width="100"
								maxlength="4"></u--input>
						</view>
						<image mode="aspectFill" @click="getImgCode" :src="img_code" class="img_code"></image>
					</view>

				</u-form-item>
				<u-form-item label="内容" prop="content" borderBottom ref="item1">
					<u--textarea placeholder="请输入内容" confirmType="done" count maxlength="500"
						v-model="params.content"></u--textarea>
				</u-form-item>
			</u--form>

			<view class="add-btn" @click="submit">提交</view>
		</view>

		<CommonTk ref="commonTk"></CommonTk>
	</view>
</template>

<script>
	import CommonTk from '@/components/CommonTk.vue'
	import Footer from '@/components/Footer.vue'
	import {
		api,
		apiBaseUrl,
		http
	} from '@/config/common.js'
	export default {
		data() {
			return {
				isTj: true,
				img_code: '',
				params: {
					name: '',
					contact: '',
					code: '',
					content: ''
				},
				rules: {
					'name': {
						type: 'string',
						required: true,
						message: '请输入标题',
						trigger: ['blur', 'change']
					},
					'contact': {
						type: 'string',
						required: true,
						message: '请输入姓名',
						trigger: ['blur', 'change']
					},
					'code': {
						type: 'string',
						required: true,
						message: '请输入验证码',
						trigger: ['blur', 'change']
					},
					'content': {
						type: 'string',
						required: true,
						message: '请输入内容',
						trigger: ['blur', 'change']
					},
				},
			}
		},
		components: {
			Footer,
			CommonTk
		},
		filters: {},
		mounted() {},
		onLoad(e) {
			this.getImgCode()
		},
		onShow() {},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		},
		beforeDestroy() {},
		onPageScroll(e) {},
		methods: {
			openPopUp() {
				this.$refs.commonTk.openShow()
			},
			submit() {
				if (!this.isTj) {
					return;
				}
				this.isTj = false
				let that = this
				// 如果有错误，会在catch中返回报错信息数组，校验通过则在then中返回true
				that.$refs.uForm.validate().then(res => {
					let p = this.params
					api.post('api/index/addLevelMsg', p).then(res => {
						let rst = res.data
						if (rst.code == 1) {
							uni.$u.toast('添加成功')
							setTimeout(function() {
								that.isTj = true
								that.params = {}
							}, 2000);
						} else {
							that.isTj = true
							uni.$u.toast(rst.msg)
							// this.getImgCode()
						}
					}).catch(err => {})
				}).catch(errors => {
					that.isTj = true
					uni.$u.toast(errors[0]['message'])
				})
			},
			getImgCode() {
				api.post('api/index/crateCode', {}).then(res => {
					let rst = res.data
					if (rst.code == 1) {
						this.img_code = rst.data
					}
				}).catch(err => {})
			},
		}
	};
</script>

<style scoped>
	.img_code {
		width: 260rpx;
		height: 60rpx;
	}

	.add-btn {
		width: 690rpx;
		height: 88rpx;
		background: #535b8a;
		border-radius: 12rpx;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #FFFFFF;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		margin: 0 auto;
		margin-top: 60rpx;
	}

	.add-box {
		width: 690rpx;
		margin: 0 auto;
	}

	.ll {
		height: 100rpx;
	}

	::v-deep .u-navbar__content {
		width: 750rpx;
		height: 88rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 2rpx 14rpx 0rpx rgba(4, 0, 0, 0.14);
	}

	.nav-right {
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.logo {
		width: 306rpx !important;
		height: 55rpx !important;
	}

	page {
		background-color: #fff;
		padding-bottom: 90rpx;
	}
</style>